﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Windows.Forms.DataVisualization.Charting;
using System.IO;

namespace ZG_member
{
    public partial class Intelligencerole : Form
    {
        public Intelligencerole()
        {
            InitializeComponent();
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
        }
        BLL.memberInfo m = new BLL.memberInfo();
        List<string> tishi = new List<string>();
        List<string> nianlinduan = new List<string>() { "0-16岁", "17-22岁", "23-30岁", "31-40岁", "41-50岁", "50岁以上" };
        List<string> huiyuandengji = new List<string>();
        List<string> xingbie = new List<string>() { "男","女"};
        List<string> jiguan = new List<string>();
        private void Intelligencerole_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            StreamReader writer = new StreamReader(Application.StartupPath + "\\zhinengjuece.txt");

            string a = writer.ReadLine();
            writer.Close();
            this.cbokaiqi.Checked = a == "1" ? true : false;
           
            Series s1 = new Series("消费总额");
            chart1.Series.Add(s1);
            chart1.Series["消费总额"].IsValueShownAsLabel = true;
            //====图表部分
            DataTable dbds;
            dbds = m.GetListSql("select  convert(varchar,date ) dateU,sum(consumeMoney) as 消费,sum(savingMoney) as 储值 from wasteBook where date>='" + DateTime.Now.AddDays(-6) + "' and date<='" + DateTime.Now + "' group by date").Tables[0];
            




            //DataTable dt = memInfo.GetListSql("select convert(varchar,DATEPART (YYYY , date ))+ '年' + convert(varchar,DATEPART (MM , date )) as dateU,sum(consumeMoney) as 消费,sum(savingMoney) as 储值 from wasteBook where date>='" + dtpStart.Value + "' and date<='" + dtpEnd.Value + "' group by DATEPART (YYYY , date ),DATEPART (" + unit + " , date ) order by DATEPART (YYYY , date ),DATEPART (" + unit + " , date )").Tables[0];

            chart1.DataSource = null;

            chart1.DataSource = dbds;
            chart1.Series["消费总额"].XValueMember = "dateU";
            chart1.Series["消费总额"].YValueMembers = "消费";
            chart1.DataBind();



            if (dbds.Rows.Count > 9) //当X轴超过9时就会显示不完全
            {
                //设置X轴坐标的间隔为1
                chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
                //设置X轴坐标的偏移为1
                chart1.ChartAreas["ChartArea1"].AxisX.IntervalOffset = 1;
                //设置交错显示，例如数据多时可分两行显示
                chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.IsStaggered = true;
            }


            //====智能生日部分
            DataSet ds;
            ds = m.GetListSql("select month(birthDay) as 月, count(month(birthDay)) as 数量 from dbo.memberInfo  group by month(birthDay)");
            decimal zonghe = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                zonghe = zonghe + decimal.Parse(ds.Tables[0].Rows[i]["数量"].ToString());
            }
            ds = m.GetListSql("select month(birthDay) as 月, count(month(birthDay)) as 数量 from dbo.memberInfo where month(birthDay) = "+DateTime.Now.Month+" group by month(birthDay)");
            string aa = "";
            if (ds.Tables[0].Rows.Count != 0)
            {
                aa = ((100 / zonghe) * decimal.Parse(ds.Tables[0].Rows[0]["数量"].ToString())).ToString("F1");
                tishi.Add("生日在本月份的会员占总数的" + aa + "%，建议您予以注意，给予适应人文关怀可能会取得意想不到的效果！");
            }
            else
            {
                tishi.Add("生日在本月份的会员占总数的0%，请您注意。");
            }

            //=====智能年龄部分
            int n = new System.Random().Next(0, nianlinduan.Count);
            DataSet ds1;
            ds1 = m.GetListSql("select datediff(year,birthDay,getdate()) as 年龄,COUNT(datediff(year,birthDay,getdate())) as 人数 from dbo.memberInfo group by datediff(year,birthDay,getdate())");
            decimal zonghe1 = 0;
            for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
            {
                zonghe1 = zonghe1 + decimal.Parse(ds1.Tables[0].Rows[i]["人数"].ToString());
            }
            int shu = nianlinduan[n].IndexOf("岁");
            string newage = nianlinduan[n].Remove(shu);
            string[] age = newage.Split('-');
            if (age.Length==1)
            {
                ds1 = m.GetListSql("select datediff(year,birthDay,getdate()) as 年龄,COUNT(datediff(year,birthDay,getdate())) as 人数 from dbo.memberInfo where datediff(year,birthDay,getdate()) > '" + age[0] + "' group by datediff(year,birthDay,getdate())");
            }
            else
            {
                ds1 = m.GetListSql("select datediff(year,birthDay,getdate()) as 年龄,COUNT(datediff(year,birthDay,getdate())) as 人数 from dbo.memberInfo where datediff(year,birthDay,getdate()) > '" + age[0] + "' and datediff(year,birthDay,getdate()) <='" + age[1] + "' group by datediff(year,birthDay,getdate())");
            }
            if (ds1.Tables[0].Rows.Count != 0)
            {
                aa = ((100 / zonghe1) * decimal.Parse(ds1.Tables[0].Rows[0]["人数"].ToString())).ToString("F1");
                tishi.Add(nianlinduan[n] + "年龄段占总数的" + aa + "%，建议您采取相对促销措施。");
            }
            else
            {
                tishi.Add(nianlinduan[n] + "年龄段占总数的0%，请您注意。");
            }

            //=====智能会员等级部分
            BLL.consumeSolution cm = new BLL.consumeSolution();
            List<Model.consumeSolution> cs  = cm.GetModelList("");
            n = new System.Random().Next(0,cs.Count);
            for (int i = 0; i < cs.Count; i++)
            {
                huiyuandengji.Add(cs[i].solutionName);
            }

            DataSet ds2;
            ds2 = m.GetListSql("select c.solutionName as 会员等级, count(cardtype) as 数量 from dbo.memberInfo m inner join dbo.consumeSolution c on(c.id = m.cardType) group by  cardtype, c.solutionName");
            zonghe = 0;
            for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
            {
                zonghe = zonghe + decimal.Parse(ds2.Tables[0].Rows[i]["数量"].ToString());
            }
            ds2 = m.GetListSql("select c.solutionName as 会员等级, count(cardtype) as 数量 from dbo.memberInfo m inner join dbo.consumeSolution c on(c.id = m.cardType) where c.solutionName = '"+huiyuandengji[n]+"' group by  cardtype, c.solutionName");
            if (ds2.Tables[0].Rows.Count != 0)
            {
                aa = ((100 / zonghe) * decimal.Parse(ds2.Tables[0].Rows[0]["数量"].ToString())).ToString("F1");
                tishi.Add(huiyuandengji[n] + "会员占总数的" + aa + "%建议您对此类型或其他类型会员进行相应鼓励，以促进消费！");
            }
            else
            {
                tishi.Add(huiyuandengji[n] + "会员占总数的0%，请您注意。");
            }
            
            //=======智能会员性别部分

            n = new System.Random().Next(0, xingbie.Count);
            DataSet ds3;
            ds3 = m.GetListSql("select m.sex as 性别, count(m.sex) as 数量 from dbo.memberInfo m inner join dbo.consumeSolution c on(c.id = m.cardType) group by  m.sex");
             zonghe1 = 0;
            for (int i = 0; i < ds3.Tables[0].Rows.Count; i++)
            {
                zonghe1 = zonghe1 + decimal.Parse(ds3.Tables[0].Rows[i]["数量"].ToString());
            }
            //int shu = nianlinduan[n].IndexOf("岁");
            //string newage = nianlinduan[n].Remove(shu);
            //string[] age = newage.Split('-');
            //if (age.Length == 1)
            //{
            //    ds3 = m.GetListSql("select datediff(year,birthDay,getdate()) as 年龄,COUNT(datediff(year,birthDay,getdate())) as 人数 from dbo.memberInfo where datediff(year,birthDay,getdate()) > '" + age[0] + "' group by datediff(year,birthDay,getdate())");
            //}
            //else
            //{
            int xinbie = xingbie[n] == "男" ? 0 : 1;
            ds3 = m.GetListSql("select m.sex as 性别, count(m.sex) as 数量 from dbo.memberInfo m inner join dbo.consumeSolution c on(c.id = m.cardType) where m.sex = " + xinbie + " group by  m.sex");
            //}
            if (ds3.Tables[0].Rows.Count != 0)
            {
                aa = ((100 / zonghe1) * decimal.Parse(ds3.Tables[0].Rows[0]["数量"].ToString())).ToString("F1");
                tishi.Add(xingbie[n] + "性会员占总数的" + aa + "%，建议您采取相对促销措施。");
            }
            else
            {
                tishi.Add(xingbie[n] + "性会员占总数的0%，请您注意。");
            }
            //========智能会员籍贯部分
            BLL.Areas ar = new BLL.Areas();
            DataSet are = ar.GetList("");
            n = new System.Random().Next(0, are.Tables[0].Rows.Count);
            for (int i = 0; i < are.Tables[0].Rows.Count; i++)
            {
                jiguan.Add(are.Tables[0].Rows[i]["AreaName"].ToString());
            }

            DataSet ds4;
            ds4 = m.GetListSql("select a.AreaName as 籍贯,COUNT(m.AreasID) as 数量 from dbo.memberInfo m inner join dbo.Areas a on(a.id = m.AreasID) group by m.AreasID,a.AreaName");
            zonghe = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                zonghe = zonghe + decimal.Parse(ds4.Tables[0].Rows[i]["数量"].ToString());
            }
            ds4 = m.GetListSql("select a.AreaName as 籍贯,COUNT(m.AreasID) as 数量 from dbo.memberInfo m inner join dbo.Areas a on(a.id = m.AreasID) where a.AreaName ='" + jiguan[n]+ "' group by m.AreasID,a.AreaName");
            if (ds4.Tables[0].Rows.Count != 0)
            {
                aa = ((100 / zonghe) * decimal.Parse(ds4.Tables[0].Rows[0]["数量"].ToString())).ToString("F1");
                tishi.Add(jiguan[n] + "会员人数占总数的" + aa + "%建议您对此类型或其他类型会员进行相应鼓励，以促进消费！");
            }
            else
            {
                tishi.Add(jiguan[n] + "会员人数占总数的0%，请您注意。");
            }
            //=====智能提醒 1
            tishi.Add("我们为您准备好了近期新增会员数量查询可以按日、月、季度、年显示");
            //=====智能提醒 2
            tishi.Add("您可以查看对店铺贡献最高的前N名");
            //=====智能提醒 3
            tishi.Add("我们为您准备了本年份的充值、消费折线统计图");
            //=====智能提醒 4
            tishi.Add("点击下面的连接可以按日、月、季度、年查询店铺的总充值额和消费额");
            //=====智能提醒 5
            tishi.Add("商品查询图表可以看到商品在一段时间内的销售走势");
            //=====智能提醒 6
            tishi.Add("商品饼图查询中的按类型查询可看到各商品系列占总数的百分比");
            //=====智能提醒 7
            tishi.Add("商品饼图查询中的按商品查询可看到各商品占总数的百分比");


            int xianshi = new System.Random().Next(0, tishi.Count);

            num = xianshi;

            this.lbltishi.Text = tishi[num];
        }
        int num = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }
        }
        int flag = 0;

        private void Intelligencerole_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (num==0)
            {
                this.lbltishi.Text = tishi[tishi.Count()-1];
                num = tishi.Count() - 1;
                return;
            }
            //num = num - 1;
            this.lbltishi.Text = tishi[--num];
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (num == tishi.Count()-1)
            {
                this.lbltishi.Text = tishi[0];
                num = 0;
                return;
            }
            //num = num + 1;
            this.lbltishi.Text = tishi[++num];
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            switch (num)
            {
                case 0:
                    AgeanalyseChart ac = new AgeanalyseChart();
                    ac.tabControl1.SelectedIndex = 1;
                    ac.ShowDialog();
                    break;
                case 1:
                    AgeanalyseChart alc = new AgeanalyseChart();
                    alc.ShowDialog();
                    break;
                case 2:
                    MembercartTypeChart mc = new MembercartTypeChart();
                    mc.ShowDialog();
                    break;
                case 3:
                    MembercartTypeChart mcc = new MembercartTypeChart();
                    mcc.ShowDialog();
                    break;
                case 4:
                    MembercartTypeChart mccc = new MembercartTypeChart();
                    mccc.ShowDialog();
                    break;
                //我们为您准备好了近期新增会员数量查询可以按日、月、季度、年显示
                case 5:
                    CustomerChart cc = new CustomerChart();
                    cc.ShowDialog();
                    break;
                //您可以查看对店铺贡献最高的前N名
                case 6:
                    CustomerContributeChart ccc = new CustomerContributeChart();
                    ccc.ShowDialog();
                    break;
                //我们为您准备了本年份的充值、消费折线统计图
                case 7:
                    RechargeMoneyChart rmc = new RechargeMoneyChart();
                    rmc.tabControl1.SelectedIndex = 2;
                    rmc.sousuo();
                    rmc.ShowDialog();
                    break;
                //点击下面的连接可以按日、月、季度、年查询店铺的总充值额和消费额
                case 8:
                    RechargeMoneyChart rm = new RechargeMoneyChart();
                    rm.tabControl1.SelectedIndex = 1;
                    rm.sousuo();
                    rm.ShowDialog();
                    break;
                //商品查询图表可以看到商品在一段时间内的销售走势
                case 9:
                    GoodsChart cg = new GoodsChart();
                    cg.tabControl1.SelectedIndex = 2;
                    cg.sousuo();
                    cg.ShowDialog();
                    break;
                //商品饼图查询中的按类型查询可看到各商品系列占总数的百分比
                case 10:
                    GoodsChart_two cct = new GoodsChart_two();
                    cct.cbotype.SelectedIndex = 0;
                    //cct.sousuo();
                    cct.ShowDialog();
                    

                    
                    break;
                //商品饼图查询中的按商品查询可看到各商品占总数的百分比
                case 11:
                     GoodsChart_two cctt = new GoodsChart_two();
                     cctt.cbotype.SelectedIndex = 1;
                     //cctt.sousuo();
                     cctt.ShowDialog();
                     
                    break;
                default:
                    break;
            }
            
        }

        private void cbokaiqi_CheckedChanged(object sender, EventArgs e)
        {
            string kaiqi = this.cbokaiqi.Checked == true ? "1" : "0";
            StreamWriter sw = new StreamWriter(Application.StartupPath + "\\zhinengjuece.txt", false, Encoding.GetEncoding("gb2312"));
            sw.Flush();
            sw.Write(kaiqi);
            sw.Close();
        }

        
    }
}
